<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Models
 * @see 		Customers
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Customers_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show_customers($uri, $num_rows= false)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['limit'] == '') $uri['limit'] = 50;
		$this->db->from('customers AS u');
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(u.username LIKE "%%%s%%" OR u.fullname LIKE "%%%s%%" OR u.code LIKE "%%%s%%" OR u.email LIKE "%%%s%%" OR u.phone LIKE "%%%s%%")', $uri['q'], $uri['q'], $uri['q'], $uri['q'], $uri['q'], $uri['q']));
		}
		$this->db->where('u.deleted', $uri['deleted']);
		if ($num_rows)
		{
			$this->db->select('id');
			return $this->db->get()->num_rows();
		}
		else
		{
			$last_login = $GLOBALS['user']['last_login'];
			$this->db->select('u.*, g.name_vn AS nhom, g.chietkhau AS chietkhau, IF ('.sprintf('u.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
			$this->db->join('customergroups AS g', 'u.level = g.id', 'left');
			if ($uri['orderby'] == '') $uri['orderby'] = 'id';
			if ($uri['ordermode'] == '') $uri['ordermode'] = 'desc';
			$this->db->order_by($uri['orderby'].' '.$uri['ordermode']);
			$this->db->limit($uri['limit'], $uri['rowstart']);
			$query = $this->db->get();
			if ($query->num_rows() > 0)
			{
				return $query->result_array();
			}
			else
			{
				return false;
			}
		}
	}

	public function show_groups($uri)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('customergroups AS g');
		if ($uri['q'] != '')
		{
			$this->db->like('g.name_vn', $uri['q']);
		}
		$last_login = $GLOBALS['user']['last_login'];
		$this->db->select('g.*, COUNT(c.id) AS khachhang, IF ('.sprintf('g.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
		$this->db->join('customers AS c', 'c.level = g.id', 'left');
			$this->db->group_by('id');
		$this->db->where('g.deleted', $uri['deleted']);
		$this->db->order_by('id desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('customers');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('customers');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function customergroups()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, name_vn');
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		$this->db->order_by('id DESC');
		$query = $this->db->get('customergroups');
		$groups = array('' => 'Chọn ...');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$groups[$row['id']] = $row['name_vn'];
			}
		}
		return $groups;
	}

	public function regions()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, name_vn');
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		$this->db->where('parent', 0);
		$this->db->order_by('id DESC');
		$query = $this->db->get('regions');
		$groups = array('' => 'Chọn ...');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$groups[$row['id']] = $row['name_vn'];
			}
		}
		return $groups;
	}

	public function branchs()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, name_vn');
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		$this->db->order_by('name_vn ASC');
		$query = $this->db->get('branchs');
		$groups = array('' => 'Chọn ...');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$groups[$row['id']] = $row['name_vn'];
			}
		}
		return $groups;
	}

	public function process_group($data, $id = '')
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('customergroups', $data);
			write_log('update_customergroups', $data['name_vn']);
		}
		else
		{
			$this->db->insert('customergroups', $data);
			write_log('add_customergroups', $data['name_vn']);
		}
		return true;
	}

	public function process_customer($data, $id = '')
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('customers', $data);
			write_log('update_customers', $data['fullname']);
		}
		else
		{
			$this->db->insert('customers', $data);
			write_log('add_customers', $data['fullname']);
		}
		return true;
	}

}

/* End of file customers_model.php */
/* Location: ./application/models/customers_model.php */